﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.ComponentModel;

namespace SalaryChannel
{
    public class PSSOrderEnity : IDisposable, INotifyPropertyChanged
    {
        private int _orderID=-1;

        public int OrderID
        {
            get { return _orderID; }
            set 
            {
                if (_orderID != value)
                {
                    _orderID = value;
                    OnPropertyChanged("OrderID");
                }
            }
        }

        private short _providerID = -999;

        public short ProviderID
        {
            get { return _providerID; }
            set {
                if (_providerID != value)
                {
                    _providerID = value;
                    OnPropertyChanged("ProviderID");
                }
            }
        }



        private short _orderStatus = -1;

        public short OrderStatus
        {
            get { return _orderStatus; }
            set {
                if (_orderStatus != value)
                {
                    _orderStatus = value;
                    OnPropertyChanged("OrderStatus");
                }
            
            }
        }




        private DateTime _orderTime=DateTime.Now;

        public DateTime OrderTime
        {
            get { return _orderTime; }
            set
            {
                if (_orderTime != value)
                {
                    _orderTime = value;
                    OnPropertyChanged("OrderTime");
                }
            }
        }


        private string _orderChannel = string.Empty;

        public string OrderChannel
        {
            get { return _orderChannel; }
            set
            {
                if (_orderChannel != value)
                {
                    _orderChannel = value;
                    OnPropertyChanged("OrderChannel");
                }
            }
        }



        private string _postcode = string.Empty;

        public string Postcode
        {
            get { return _postcode; }
            set
            {
                if (_postcode != value)
                {
                    _postcode = value;
                    OnPropertyChanged("Postcode");
                }
            }
        }

        private string _tel = string.Empty;

        public string Tel
        {
            get { return _tel; }
            set
            {
                if (_tel != value)
                {
                    _tel = value;
                    OnPropertyChanged("Tel");
                }
            }
        }

        private string _address = string.Empty;

        public string Address
        {
            get { return _address; }
            set
            {
                if (_address != value)
                {
                    _address = value;
                    OnPropertyChanged("Address");
                }
            }
        }

        public int TotalCount
        {
            get
            {
                int count = 0;
                if (_deviceList.Count > 0)
                {
                    foreach (OrderDeviceEnity d in _deviceList)
                    {
                        count += d.Total;
                    }
                }
                return count;

            }
        }
        /// <summary>
        /// 元
        /// </summary>
        public decimal TotalValue
        {
            get
            {
                decimal count = 0;
                if (_deviceList.Count > 0)
                {
                    foreach (OrderDeviceEnity d in _deviceList)
                    {
                        count += d.Total_Value;
                    }
                }
                return count;
            }
        }
        private string _chief = string.Empty;

        public string Chief
        {
            get { return _chief; }
            set
            {
                if (_chief != value)
                {
                    _chief = value;

                    OnPropertyChanged("Chief");
                }
            }
        }

        private string _createID = string.Empty;

        public string CreateID
        {
            get { return _createID; }
            set { _createID = value; }
        }



        private List<OrderDeviceEnity> _deviceList = new List<OrderDeviceEnity>();

        public List<OrderDeviceEnity> DeviceList
        {
            get { return _deviceList; }
            set
            {
                if (_deviceList != value)
                {
                    _deviceList = value;
                    OnPropertyChanged("DeviceList");
                    OnPropertyChanged("TotalCount");
                    OnPropertyChanged("TotalValue");
                }
            }
        }

        public PSSOrderEnity()
        {
            //this.OrderID = Guid.NewGuid().ToString();

        }

        public void RemoveDevice(int DeviceID)
        {
            if (_deviceList == null
                || _deviceList.Count == 0) return;
            foreach (OrderDeviceEnity d in _deviceList)
            {
                if (d.DeviceID == DeviceID)
                {
                    _deviceList.Remove(d);
                    break;
                }
            }
        }
        public void RemoveDevice(string DeviceID)
        {
            RemoveDevice(int.Parse(DeviceID));
        }

        public int AddDevice(OrderDeviceEnity device)
        {

            if (IsOrdered(device.DeviceID)) return 1;

            _deviceList.Add(device);
            return 0;
        }

        public bool IsOrdered(int DeviceID)
        {
            foreach (OrderDeviceEnity d in _deviceList)
            {
                if (d.DeviceID == DeviceID)
                {
                    return true;
                }
            }
            return false;
        }

        public void Clear()
        {
            _orderID = -1;
            _providerID = -1;
            _tel = string.Empty;
            _postcode = string.Empty;
            _orderStatus = -1;
            _orderChannel = string.Empty;
            _deviceList.Clear();
            _chief = string.Empty;
            _address = string.Empty;
            _createID = string.Empty;
        }



        #region IDisposable 成员

        public void Dispose()
        {
            if(_deviceList!=null && _deviceList.Count>0)
                 _deviceList.Clear();

            _deviceList = null;
        }

        #endregion

        #region INotifyPropertyChanged 成员

        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = this.PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion
    }

    public class OrderDeviceEnity
    {
        private int _deviceID = -1;

        public int DeviceID
        {
            get { return _deviceID; }
            set { _deviceID = value; }
        }

        private string _deviceName=string.Empty;

        public string DeviceName
        {
            get { return _deviceName; }
            set { _deviceName = value; }
        }

        private decimal _price = 0;

        /// <summary>
        /// 元
        /// </summary>
        public decimal Price
        {
          get { return _price; }
          set { _price = value; }
        }
      

        private int _total=0;

        public int Total
        {
          get { return _total; }
          set { _total = value; }
        }


        public decimal Total_Value
        {
            get
            {
                return Price* Total;
            }
        }

        private int _feePack=0;

        public int FeePack
        {
          get { return _feePack; }
          set { _feePack = value; }
        }
               

        private string _remark;

        public string Remark
        {
          get { return _remark; }
          set { _remark = value; }
        }
       
        public string ShowRemark
        {
            get
            {
                return (Remark + "[" + FeePack.ToString() + "元话费包]");
            }
        }
    }
}